package com.taotao.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.CookieUtils;
import com.taotao.pojo.TbUser;
import com.taotao.sso.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/check/{param}/{type}")
	@ResponseBody()
	public Object checkData(@PathVariable String param,@PathVariable Integer type,String callback) {

		if(StringUtils.isBlank(param))
			return TaotaoResult.build(400, "校验数据不能为空");
		if(type==null)
			return TaotaoResult.build(400, "类型不能为空");
		if(type!=1 && type!=2 && type!=3) {
			return TaotaoResult.build(400, "错误的类型");
		}
		
		TaotaoResult result = userService.checkData(param, type);
	
		return jsonpCallback(callback, result);
		
	}
	
	@RequestMapping("/register")
	@ResponseBody
	public Object createUser(TbUser user,String callback) {
		if(user.getUsername()==null || user.getPassword()==null)
			return TaotaoResult.build(400, "用户名或密码不能为空");
		
		TaotaoResult result = userService.insertUser(user);
		return jsonpCallback(callback, result);
		
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public Object userLogin(HttpServletRequest request,HttpServletResponse response,String username,String password,String callback) {
		if(StringUtils.isBlank(username) || StringUtils.isBlank(password))
			return TaotaoResult.build(400,"用户名或密码不能为空");
		
		TaotaoResult result=null;
		try {
			result = userService.userLogin(request, response, username, password);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return jsonpCallback(callback, result);
	}
	
	@RequestMapping("/token/{token}")
	@ResponseBody
	public Object getUserByToken(@PathVariable String token,String callback) {
		if(StringUtils.isBlank(token)) {
			return TaotaoResult.build(400, "用户还未登陆");
		}
		TaotaoResult result=null;
		try {
			result = userService.getUserByToken(token);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return jsonpCallback(callback, result);
	}
	
	@RequestMapping("/logout/{token}")
	@ResponseBody
	public Object userLogout(HttpServletRequest request,HttpServletResponse response,@PathVariable String token,String callback) {
		TaotaoResult result = userService.userLogout(request,response,token);
		return jsonpCallback(callback, result);
	}
	
	
	private Object jsonpCallback(String callback,TaotaoResult result) {
		if(StringUtils.isBlank(callback))
			return result;
		MappingJacksonValue jacksonValue = new MappingJacksonValue(result);
		jacksonValue.setJsonpFunction(callback);
		return jacksonValue;
	}
}
